package com.tpc.shuyu.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.tpc.shuyu.utils.JDBCUtil;
import com.tpc.shuyu.utils.TableSaveUtil;
import com.tpc.shuyu.utils.XMLUtil;
import lombok.extern.slf4j.Slf4j;
import org.jdom.Document;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URI;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

@RequestMapping("/renhong")
@Controller
@ResponseBody
@Slf4j

public class OrderDelController {
//    @PostMapping(value = "/orderDel",consumes = "application/soap+xml;charset=utf-8")
//    public String sendOrderDelToSK(URI baseUri, @RequestBody String param) {
//        log.info("{},{}",baseUri,param);
//        JSONObject returnJSON = new JSONObject();
//        returnJSON.put("ReturnValue",0);
//        returnJSON.put("ReturnMsg", "");
//        Document document = XMLUtil.strXmlToDocument(param);
//        String response = XMLUtil.getValueByElementName(document, "jsonStr");
//        JSONObject jsonObject=  JSONObject.parseObject(response);
//        JSONArray orderArray = new JSONArray();
//        orderArray.add(jsonObject);
//
//        try {
//            Connection connection = JDBCUtil.getConnection();
//            TableSaveUtil.tableSave(connection,"SY_ORDER_DEL",orderArray);
//        }catch (Exception e){
//            returnJSON.put("ReturnValue",-1);
//            returnJSON.put("ReturnMsg",e.getMessage());
//            e.printStackTrace();
//        }
//
//
//        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><O2oOrderAcceptResponse xmlns=\"http://tempuri.org/\"><O2oOrderAcceptResult>:O2oOrderAcceptResult</O2oOrderAcceptResult></O2oOrderAcceptResponse></soap:Body></soap:Envelope>";
//        xml=xml.replaceAll(":O2oOrderAcceptResult",JSONObject.toJSONString(returnJSON));
//        return xml;
//    }

    @PostMapping(value = "/orderDel")
    public String sendOrderDelToSK(URI baseUri, @RequestBody String param) {
        log.info("{},{}",baseUri,param);
        JSONObject returnJSON = new JSONObject();
        returnJSON.put("ReturnValue",0);
        returnJSON.put("ReturnMsg", "");
        Document document = XMLUtil.strXmlToDocument(param);
        String response = XMLUtil.getValueByElementName(document, "jsonStr");
        JSONObject jsonObject=  JSONObject.parseObject(response);
        JSONArray orderArray = new JSONArray();
        orderArray.add(jsonObject);
        Connection connection = null;
        try {
            connection = JDBCUtil.getConnection();
            TableSaveUtil.tableSave(connection,"SY_ORDER_DEL",orderArray);
        }catch (Exception e){
            returnJSON.put("ReturnValue",-1);
            returnJSON.put("ReturnMsg",e.getMessage());
            e.printStackTrace();
        }finally {
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }


        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><O2oOrderAcceptResponse xmlns=\"http://tempuri.org/\"><O2oOrderAcceptResult>:O2oOrderAcceptResult</O2oOrderAcceptResult></O2oOrderAcceptResponse></soap:Body></soap:Envelope>";
        xml=xml.replaceAll(":O2oOrderAcceptResult",JSONObject.toJSONString(returnJSON));
        return xml;
    }
}
